﻿using System;
using System.IO;
using System.Security.Cryptography;
using AIS.Framework.Properties;

namespace AIS.Framework
{
	public static class Globals
	{
		#region Static Fields

		public static bool LogInFile = Settings.Default.LogInFile;
		public static double CellLifespan = Settings.Default.CellLifespan;		// Lifespan of all cells

		public static double NSAffinityThreashold = Settings.Default.NSAffinityThreshold;
		public static double HelperAffinityThreashold = Settings.Default.HelperAffinityThreshold;
		public static double APCAffinityThreshold = Settings.Default.APCAffinityThreshold;
		public static double APCMigrationThreshold = Settings.Default.APCMigrationThreshold;			// APC migration threshold: Collected concentration of danger signals
		public static int HelperActivationThreshold = Settings.Default.HelperActivationThreshold;		// Helper T cell activation threshold: Collected concentration of costimulation signals 
		public static double SignalDispatchCoef = Settings.Default.SignalDispatchCoef;
		public static double CellSignalAgeCost = Settings.Default.CellSignalAgeCost;
		public static int FullDispatchConcentrationThreshold = 8;

		public static int DangerSignalWeight = Settings.Default.DangerSignalWeight;	// TODO: update by ICARIS'05 "Introducing Dendritic Cells as a Novel Immune-inspired..."
		public static int SafeSignalWeight = Settings.Default.SafeSignalWeight;		// TODO: update by ICARIS'05 "Introducing Dendritic Cells as a Novel Immune-inspired..."
		public static int PAMPWeight = Settings.Default.PAMPWeight;			// TODO: update by ICARIS'05 "Introducing Dendritic Cells as a Novel Immune-inspired..."
		//public static int	APCMaturationThreashold = 10;					// TODO: update by ICARIS'05 "Introducing Dendritic Cells as a Novel Immune-inspired..."

		public const int AddressingPort = 9051;			// Default addressing listening port number
		public const string DefaultRemotingUsername = "AISIDS_Remoting";
		public const string DefaultRemotingPassword = "E3OnG_WpQ$74H-CjDF~43t";
		public static readonly byte[] DefaultEncryptionKey = { 0xf9, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0xe5, 0x64, 0x76, 0x45, 0x64, 0x05, 0x7b, 0x61, 0x15, 0xd5 };
		public static readonly byte[] DefaultEncryptionIV = { 0x83, 0xa4, 0xf2, 0xfe, 0xc0, 0x77, 0xa0, 0x0e, 0x91, 0x63, 0x94, 0x9b, 0x3d, 0xa4, 0x6e, 0x99 };

		// NOTE: random numbers generated by this RNG are used as seeds for RNGs of agents of the system
		// this mechanism is used to initialize dissimilar random sequences for various agents of the system
		private static Random randGen = new Random();

		#endregion

		#region Static Methods

		public static int GetRandomSeed()
		{
			return randGen.Next(Int32.MaxValue);
		}

		public static byte[] Encrypt(byte[] clearData)
		{
			MemoryStream ms = new MemoryStream();
			Rijndael alg = Rijndael.Create();

			alg.Key = Globals.DefaultEncryptionKey;
			alg.IV = Globals.DefaultEncryptionIV;
			CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);

			cs.Write(clearData, 0, clearData.Length);
			cs.Close();
			return ms.ToArray();
		}

		public static byte[] Decrypt(byte[] cipherData)
		{
			MemoryStream ms = new MemoryStream();
			Rijndael alg = Rijndael.Create();

			alg.Key = Globals.DefaultEncryptionKey;
			alg.IV = Globals.DefaultEncryptionIV;

			CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);

			cs.Write(cipherData, 0, cipherData.Length);
			cs.Close();
			return ms.ToArray();
		}

		#endregion
	}
}
